package jsr292.weaver;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class Utils {
  public static final String JAVA_LANG_INVOKE = "java/lang/invoke";
  public static final String JAVA_LANG_INVOKE_DOT_PREFIX = "java.lang.invoke.";
  public static final String JAVA_METHOD_HANDLE = JAVA_LANG_INVOKE + "/MethodHandle";
  public static final String JAVA_METHOD_HANDLES = JAVA_LANG_INVOKE + "/MethodHandles";
  public static final String JAVA_LOOKUP = JAVA_LANG_INVOKE + "/MethodHandles$Lookup";
  public static final String JAVA_LOOKUP_DESC = 'L' + JAVA_LOOKUP + ';';
  public static final String JAVA_LANG_CLASS_VALUE = "java/lang/ClassValue";
  public static final String JAVA_LANG_UTIL_OBJECTS = "java/lang/util/Objects";
  
  public static final String JSR292_PACKAGE_PREFIX = "jsr292/";
  public static final String JSR292_PACKAGE = JSR292_PACKAGE_PREFIX + JAVA_LANG_INVOKE;
  
  public static final String JSR292_METHOD_HANDLE = JSR292_PACKAGE + "/MethodHandle";
  public static final String JSR292_METHOD_HANDLE_DESC = 'L' + JSR292_METHOD_HANDLE + ';';
  public static final String JSR292_CALLSITE = JSR292_PACKAGE + "/CallSite";
  public static final String JSR292_CALLSITE_DESC = 'L' + JSR292_CALLSITE + ';';
  public static final String JSR292_METHODTYPE = JSR292_PACKAGE + "/MethodType";
  public static final String JSR292_METHODTYPE_DESC = 'L' + JSR292_METHODTYPE + ';';
  public static final String JSR292_METHOD_HANDLES = JSR292_PACKAGE + "/MethodHandles";
  public static final String JSR292_LOOKUP = JSR292_PACKAGE + "/MethodHandles$Lookup";
  public static final String JSR292_LOOKUP_DESC = 'L' + JSR292_LOOKUP + ';';
  public static final String JSR292_CLASS_VALUE = JSR292_PACKAGE_PREFIX + "java/lang" + "/ClassValue";
  public static final String JSR292_OBJECTS = JSR292_PACKAGE_PREFIX + "java/util" + "/Objects";

  public static final String JSR292_ACCESS = JSR292_PACKAGE + "/Access";
  public static final String JSR292_CONSTANT_METHOD_HANDLES = JSR292_PACKAGE + "/ConstantMethodHandles";
  public static final String JSR292_BOOTSTRAP_CALLSITE = JSR292_PACKAGE + "/BootstrapCallSite";
  public static final String JSR292_BOOTSTRAP_CALLSITE_DESC = 'L' + JSR292_BOOTSTRAP_CALLSITE + ';';
  
  public static final int PARAMETER_THRESHOLD = 7;
  
  public static void ldcInteger(MethodVisitor visitor, int value) {
    if (value >= -1 && value <= 5) {
      visitor.visitInsn(Opcodes.ICONST_0 + value);
      return;
    }
    if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
      visitor.visitIntInsn(Opcodes.BIPUSH, value);
      return;
    }
    if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
      visitor.visitIntInsn(Opcodes.SIPUSH, value);
      return;
    }
    visitor.visitLdcInsn(value);
  }
}
